<template>
  <div>
    <el-form ref="addFormRef" :rules="rules" :model="addForm.data" label-width="120">
      <el-form-item label="岗亭名称" prop="name">
        <el-input
          v-model="addForm.data.name"
          style="width: 320px"
          placeholder="请输入岗亭名称"
        />
      </el-form-item>
      <el-form-item label="岗亭地址" prop="address">
        <el-input
          v-model="addForm.data.address"
          style="width: 320px"
          placeholder="请输入岗亭地址"
        />
      </el-form-item>
      <el-form-item label="岗亭经度" prop="longitude">
        <el-input
          v-model="addForm.data.longitude"
          style="width: 320px"
          placeholder="请输入岗亭经度"
        />
      </el-form-item>
      <el-form-item label="岗亭纬度" prop="latitude">
        <el-input
          v-model="addForm.data.latitude"
          style="width: 320px"
          placeholder="请输入岗亭纬度"
        />
      </el-form-item>
      <el-form-item label="出入类型" prop="type">
        <el-select
          v-model="addForm.data.type"
          filterable
          style="width: 320px"
          placeholder="请选择出入类型"
        >
          <el-option
            v-for="item in typeList.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          v-model="addForm.data.remark"
          maxlength="20"
          show-word-limit
          type="textarea"
          style="width: 320px"
        />
      </el-form-item>
      <el-form-item>
        <div class="footer">
          <div></div>
          <div>
            <el-button class="reset-btn" @click="resetForm(addFormRef)">重 置</el-button>
            <el-button class="confirm-btn" @click="submitForm(addFormRef)"
              >确 认</el-button
            >
          </div>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { getEntryAdd } from "../request.js";
import { useRoute } from "vue-router";
const route = useRoute();

const emits = defineEmits(["operationDone"]);

const props = defineProps({
  operationId: {
    type: Object,
    default: () => {},
  },
  listData: {
    type: Object,
    default: () => {},
  },
});

const addFormRef = ref("");
const addForm = reactive({
  data: {
    lotId: route.query.id,
    lotName:props.operationId
  },
});

const rules = reactive({
  name: [{ required: true, message: "请输入岗亭名称", trigger: "blur" }],
  address: [{ required: true, message: "请输入岗亭地址", trigger: "blur" }],
  longitude: [{ required: true, message: "请输入岗亭经度", trigger: "blur" }],
  latitude: [{ required: true, message: "请输入岗亭纬度", trigger: "blur" }],
  type: [{ required: true, message: "请选择出入类型", trigger: "change" }],
});

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      getEntryAdd(addForm.data).then((res) => {
        if (res.data.code === 0) {
          ElMessage({
            type: "success",
            message: res.data.result,
          });
          emits("operationDone");
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    }
  });
};

const resetForm = async (formEl) => {
  if (!formEl) return;
  addForm.data = {
    lotId: route.query.id,
  };
  formEl.resetFields();
};

const typeList = reactive({
  data: [],
});

onMounted(() => {
  setTimeout(() => {
    typeList.data = props.listData.typeList;
  }, 1000);
});
</script>

<style lang="less" scoped>
.footer {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.reset-btn {
  width: 65px;
  height: 32px;
  background: #ffffff;
  border-radius: 2px;
  border: 1px solid #d9d9d9;
  font-weight: 400;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.65);
}
.confirm-btn {
  width: 65px;
  height: 32px;
  background: #007aff;
  border-radius: 2px;
  font-weight: 400;
  font-size: 14px;
  color: #ffffff;
}
</style>
